package com.showtao.front.dto;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;

import com.showtao.front.domain.Product;
import com.showtao.front.domain.VehicleModelFeature;

@SuppressWarnings("serial")
public class ProductDto implements Serializable{

    private Long id;
    private String code;
    private Long categoryId;
    private String categoryName;
    private Long modelId;
    private String breadCrumb;
    private String mobileIntroPicResourceId;
    private String introPicResourceId;
    private String paramPicResourceId;
    private String dismountingPicResourceId;
    private String mergingPicResourceId;
    private String modelYear;
    private String modelParameters;
    private Double modelFactor;
    private String notes;
    private String parameters;
    private String dismountingNotes;
    private List<Long> featureIds;
    
    private Date createdAt;
    private Date updatedAt;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
    
    public Long getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(Long categoryId) {
        this.categoryId = categoryId;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    public Long getModelId() {
        return modelId;
    }

    public void setModelId(Long modelId) {
        this.modelId = modelId;
    }

    public String getBreadCrumb() {
        return breadCrumb;
    }

    public void setBreadCrumb(String breadCrumb) {
        this.breadCrumb = breadCrumb;
    }
    
    public String getMobileIntroPicResourceId() {
		return mobileIntroPicResourceId;
	}

	public void setMobileIntroPicResourceId(String mobileIntroPicResourceId) {
		this.mobileIntroPicResourceId = mobileIntroPicResourceId;
	}

	public String getModelYear() {
		return modelYear;
	}

	public void setModelYear(String modelYear) {
		this.modelYear = modelYear;
	}

	public String getModelParameters() {
		return modelParameters;
	}

	public void setModelParameters(String modelParameters) {
		this.modelParameters = modelParameters;
	}

	public Double getModelFactor() {
		return modelFactor;
	}

	public void setModelFactor(Double modelFactor) {
		this.modelFactor = modelFactor;
	}

	public String getNotes() {
		return notes;
	}

	public void setNotes(String notes) {
		this.notes = notes;
	}

	public String getParameters() {
		return parameters;
	}

	public void setParameters(String parameters) {
		this.parameters = parameters;
	}

	public String getDismountingNotes() {
		return dismountingNotes;
	}

	public void setDismountingNotes(String dismountingNotes) {
		this.dismountingNotes = dismountingNotes;
	}

	public String getIntroPicResourceId() {
		return introPicResourceId;
	}

	public void setIntroPicResourceId(String introPicResourceId) {
		this.introPicResourceId = introPicResourceId;
	}

	public String getParamPicResourceId() {
		return paramPicResourceId;
	}

	public void setParamPicResourceId(String paramPicResourceId) {
		this.paramPicResourceId = paramPicResourceId;
	}

	public String getDismountingPicResourceId() {
		return dismountingPicResourceId;
	}

	public void setDismountingPicResourceId(String dismountingPicResourceId) {
		this.dismountingPicResourceId = dismountingPicResourceId;
	}

	public String getMergingPicResourceId() {
		return mergingPicResourceId;
	}

	public void setMergingPicResourceId(String mergingPicResourceId) {
		this.mergingPicResourceId = mergingPicResourceId;
	}

	public Date getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    public Date getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(Date updatedAt) {
        this.updatedAt = updatedAt;
    }
    
    public List<Long> getFeatureIds() {
		return featureIds;
	}

	public void setFeatureIds(List<Long> featureIds) {
		this.featureIds = featureIds;
	}

	@SuppressWarnings({ "unchecked" })
	public static ProductDto from(Product product){
        ProductDto dto = new ProductDto();
        dto.setId(product.getId());
        dto.setCode(product.getCode());
        dto.setModelYear(product.getModelYear());
        dto.setModelParameters(product.getModelParameters());
        dto.setModelFactor(product.getModelFactor());
        dto.setParameters(product.getParameters());
        dto.setNotes(product.getNotes());
        dto.setDismountingNotes(product.getDismountingNotes());
        dto.setCreatedAt(product.getCreatedAt());
        dto.setUpdatedAt(product.getUpdatedAt());
        if(product.getModel()!=null){
            dto.setModelId(product.getModel().getId());
            StringBuilder sb = new StringBuilder()
                .append(product.getModel().getName());
            if(product.getModel().getLine() != null){
                sb.insert(0, product.getModel().getLine().getName()+" > ");
                if(product.getModel().getLine().getBrand() != null){
                    sb.insert(0, product.getModel().getLine().getBrand().getName()+" > ");
                }
            }
            dto.setBreadCrumb(sb.toString());
        }
        if(product.getCategory()!=null){
            dto.setCategoryId(product.getCategory().getId());
            dto.setCategoryName(product.getCategory().getName());
        }
        if(product.getIntroPic()!=null){
        	dto.setIntroPicResourceId(product.getIntroPic().getResId());
        }
        if(product.getParamPic()!=null){
        	dto.setParamPicResourceId(product.getParamPic().getResId());
        }
        if(product.getDismountingPic()!=null){
        	dto.setDismountingPicResourceId(product.getDismountingPic().getResId());
        }
        if(product.getMergingPic()!=null){
        	dto.setMergingPicResourceId(product.getMergingPic().getResId());
        }
        if(product.getMobileIntroPic()!=null){
        	dto.setMobileIntroPicResourceId(product.getMobileIntroPic().getResId());
        }
        if(product.getFeatures() !=null){
        	Collection<Long> c = CollectionUtils.collect(
        			product.getFeatures(), new Transformer(){

						@Override
						public Object transform(Object o) {
							VehicleModelFeature feature = (VehicleModelFeature)o;
							return feature != null ? feature.getId() : null;
						}
        				
        			});
        	dto.setFeatureIds(new ArrayList<Long>(c));
        }
        return dto;
    }
}
